package com.code.base.acc.action;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.struts2.ServletActionContext;

import com.code.base.acc.pojo.AccPsn;
import com.code.base.acc.service.AccPsnService;
import com.code.util.JSONUtil;
import com.code.vo.JSONVO;


public class AccPsnAction {


	@Resource
	private AccPsnService accPsnService;

	private AccPsn accPsn = new AccPsn();

	private String accPsnJson;

	private Integer limit;

	private Integer start;

	public AccPsn getAccPsn() {
		return this.accPsn;
	}

	public AccPsn setAccPsn(AccPsn accPsn) {
		return this.accPsn = accPsn;
	}

	public String getAccPsnJson() {
		return accPsnJson;
	}

	public void setAccPsnJson(String accPsnJson) {
		this.accPsnJson = accPsnJson;
		JSONObject jsonObject = JSONObject.fromObject(accPsnJson);
		JSONObject temp = new JSONObject();
		for (Object key : jsonObject.keySet()) {
			if (null != jsonObject.get(key) && !"".equals(jsonObject.get(key))) {
				temp.put(key, jsonObject.get(key));
			}
		}
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"}));
		accPsn = (AccPsn) JSONObject.toBean(temp, AccPsn.class);
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public String getDataById() {
		Long id = Long.parseLong(ServletActionContext.getRequest().getParameter("id"));

		AccPsn entity = accPsnService.getDataById(id);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO(JSONObject.fromObject(entity)));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String getAllData() {
		List<AccPsn> list = accPsnService.getAllData();

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(JSONArray.fromObject(list));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String getDataByCondition() {
		if (null != start && null != limit) {
			accPsn.setCurPage(start, limit);
			accPsn.setTotalRow(accPsnService.getDataCountByCondition(accPsn));
		}

		List<AccPsn> list = accPsnService.getDataByCondition(accPsn);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO(JSONArray.fromObject(list), accPsn));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String getDataByLikeCondition() {
		if (null != start && null != limit) {
			accPsn.setCurPage(start, limit);
			accPsn.setTotalRow(accPsnService.getDataCountByLikeCondition(accPsn));
		}

		List<AccPsn> list = accPsnService.getDataByLikeCondition(accPsn);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO(JSONArray.fromObject(list), accPsn));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String getDataByIds() {
		String[] ids = ServletActionContext.getRequest().getParameter("ids").split(",");
		Long[] temp = new Long[ids.length];
		for (int i = 0; i < ids.length; i++) {
			temp[i] = Long.parseLong(ids[i]);
		}

		List<AccPsn> list = accPsnService.getDataByIds(temp);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO(JSONArray.fromObject(list)));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String insertData() {
		accPsnService.insertData(accPsn);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String insertDataByJson() {
		String json = ServletActionContext.getRequest().getParameter("json");
		if (null != json && !"".equals(json)) {
			List<JSONObject> list = JSONUtil.jsonToList(json);
			for (JSONObject jsonObject : list) {
				AccPsn entity = (AccPsn) JSONObject.toBean(jsonObject, AccPsn.class);
				accPsnService.insertData(entity);
			}
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String updateData() {
		accPsnService.updateData(accPsn);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String updateDataByJson() {
		String json = ServletActionContext.getRequest().getParameter("json");
		if (null != json && !"".equals(json)) {
			List<JSONObject> list = JSONUtil.jsonToList(json);
			for (JSONObject jsonObject : list) {
				AccPsn entity = (AccPsn) JSONObject.toBean(jsonObject, AccPsn.class);
				accPsnService.updateData(entity);
			}
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String deleteDataByObject() {
		accPsnService.deleteDataByObject(accPsn);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String deleteDataById() {
		Long id = Long.parseLong(ServletActionContext.getRequest().getParameter("id"));

		accPsnService.deleteDataById(id);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String deleteDataByIds() {
		String[] ids = ServletActionContext.getRequest().getParameter("ids").split(",");
		Long[] temp = new Long[ids.length];
		for (int i = 0; i < ids.length; i++) {
			temp[i] = Long.parseLong(ids[i]);
		}

		accPsnService.deleteDataByIds(temp);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String deleteDataByJson() {
		String json = ServletActionContext.getRequest().getParameter("json");
		if (null != json && !"".equals(json)) {
			List<JSONObject> list = JSONUtil.jsonToList(json);
			for (JSONObject jsonObject : list) {
				AccPsn entity = (AccPsn) JSONObject.toBean(jsonObject, AccPsn.class);
				accPsnService.deleteDataById(entity.getId());
			}
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}


	public String operateDataByJson() {
		String json = ServletActionContext.getRequest().getParameter("json");
		if (null != json && !"".equals(json)) {
			JSONArray jsonArray = JSONArray.fromObject(json);
			Object[] objs = jsonArray.toArray();

			List<JSONObject> insertList = JSONUtil.jsonToList(objs[0].toString());
			List<JSONObject> updateList = JSONUtil.jsonToList(objs[1].toString());
			List<JSONObject> deleteList = JSONUtil.jsonToList(objs[2].toString());

			for (JSONObject jsonObject : insertList) {
				AccPsn entity = (AccPsn) JSONObject.toBean(jsonObject, AccPsn.class);
				accPsnService.insertData(entity);
			}
			for (JSONObject jsonObject : updateList) {
				AccPsn entity = (AccPsn) JSONObject.toBean(jsonObject, AccPsn.class);
				accPsnService.updateData(entity);
			}
			for (JSONObject jsonObject : deleteList) {
				AccPsn entity = (AccPsn) JSONObject.toBean(jsonObject, AccPsn.class);
				accPsnService.deleteDataById(entity.getId());
			}

		}

		return null;
	}


}
